package com.china08.yxyapi.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

/**
 * 日志:API执行时长
 * 
 * @author Ertao.Fang
 *
 */
@Aspect
@Component
public class LogApiExcutionTimeAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(LogApiExcutionTimeAspect.class);

    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {

        Object returnValue;
        StopWatch clock = new StopWatch(getClass().getName());

        try {
            clock.start(pjp.toShortString());
            returnValue = pjp.proceed();
        } finally {
            clock.stop();
            LOGGER.info("\n" + clock.prettyPrint());
        }

        return returnValue;

    }

}
